<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备IMEI" prop="deviceImei">
        <el-input
          v-model="queryParams.deviceImei"
          placeholder="请输入设备IMEI"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备SIM-ICCID" prop="simIccid">
        <el-input
          v-model="queryParams.simIccid"
          placeholder="请输入设备SIM-ICCID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="deviceModel">
        <el-input
          v-model="queryParams.deviceModel"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备SN" prop="deviceSn">
        <el-input
          v-model="queryParams.deviceSn"
          placeholder="请输入设备SN"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备升级状态" prop="deviceUpgrade">
        <el-input
          v-model="queryParams.deviceUpgrade"
          placeholder="请输入设备升级状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备Debug状态" prop="deviceDebug">
        <el-input
          v-model="queryParams.deviceDebug"
          placeholder="请输入设备Debug状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备固件版本" prop="deviceVer">
        <el-input
          v-model="queryParams.deviceVer"
          placeholder="请输入设备固件版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务器参数版本" prop="serverVer">
        <el-input
          v-model="queryParams.serverVer"
          placeholder="请输入服务器参数版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备参数版本" prop="paramVer">
        <el-input
          v-model="queryParams.paramVer"
          placeholder="请输入设备参数版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备参数更新时间" prop="paramUpdate">
        <el-date-picker clearable
          v-model="queryParams.paramUpdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择设备参数更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备上线时间" prop="onlineTime">
        <el-date-picker clearable
          v-model="queryParams.onlineTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择设备上线时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备保活时间" prop="keepaliveTime">
        <el-date-picker clearable
          v-model="queryParams.keepaliveTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择设备保活时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目ID" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="固件ID" prop="firmwareId">
        <el-input
          v-model="queryParams.firmwareId"
          placeholder="请输入固件ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['irtu:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['irtu:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['irtu:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['irtu:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="设备IMEI" align="center" prop="deviceImei" />
      <el-table-column label="设备SIM-ICCID" align="center" prop="simIccid" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备型号" align="center" prop="deviceModel" />
      <el-table-column label="设备SN" align="center" prop="deviceSn" />
      <el-table-column label="设备升级状态" align="center" prop="deviceUpgrade" />
      <el-table-column label="设备Debug状态" align="center" prop="deviceDebug" />
      <el-table-column label="设备固件版本" align="center" prop="deviceVer" />
      <el-table-column label="服务器参数版本" align="center" prop="serverVer" />
      <el-table-column label="设备参数版本" align="center" prop="paramVer" />
      <el-table-column label="设备参数更新时间" align="center" prop="paramUpdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paramUpdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备上线时间" align="center" prop="onlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.onlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备保活时间" align="center" prop="keepaliveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.keepaliveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="固件ID" align="center" prop="firmwareId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['irtu:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['irtu:device:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备IMEI" prop="deviceImei">
          <el-input v-model="form.deviceImei" placeholder="请输入设备IMEI" />
        </el-form-item>
        <el-form-item label="设备SIM-ICCID" prop="simIccid">
          <el-input v-model="form.simIccid" placeholder="请输入设备SIM-ICCID" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备SN" prop="deviceSn">
          <el-input v-model="form.deviceSn" placeholder="请输入设备SN" />
        </el-form-item>
        <el-form-item label="设备升级状态" prop="deviceUpgrade">
          <el-input v-model="form.deviceUpgrade" placeholder="请输入设备升级状态" />
        </el-form-item>
        <el-form-item label="设备Debug状态" prop="deviceDebug">
          <el-input v-model="form.deviceDebug" placeholder="请输入设备Debug状态" />
        </el-form-item>
        <el-form-item label="设备固件版本" prop="deviceVer">
          <el-input v-model="form.deviceVer" placeholder="请输入设备固件版本" />
        </el-form-item>
        <el-form-item label="服务器参数版本" prop="serverVer">
          <el-input v-model="form.serverVer" placeholder="请输入服务器参数版本" />
        </el-form-item>
        <el-form-item label="设备参数版本" prop="paramVer">
          <el-input v-model="form.paramVer" placeholder="请输入设备参数版本" />
        </el-form-item>
        <el-form-item label="设备参数更新时间" prop="paramUpdate">
          <el-date-picker clearable
            v-model="form.paramUpdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择设备参数更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备上线时间" prop="onlineTime">
          <el-date-picker clearable
            v-model="form.onlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择设备上线时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备保活时间" prop="keepaliveTime">
          <el-date-picker clearable
            v-model="form.keepaliveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择设备保活时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="固件ID" prop="firmwareId">
          <el-input v-model="form.firmwareId" placeholder="请输入固件ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/irtu/device";

export default {
  name: "Device",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceImei: null,
        simIccid: null,
        deviceName: null,
        deviceModel: null,
        deviceSn: null,
        deviceUpgrade: null,
        deviceDebug: null,
        deviceVer: null,
        serverVer: null,
        paramVer: null,
        paramUpdate: null,
        onlineTime: null,
        keepaliveTime: null,
        projectId: null,
        firmwareId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceImei: [
          { required: true, message: "设备IMEI不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        deviceImei: null,
        simIccid: null,
        deviceName: null,
        deviceModel: null,
        deviceSn: null,
        deviceUpgrade: null,
        deviceDebug: null,
        deviceVer: null,
        serverVer: null,
        paramVer: null,
        paramUpdate: null,
        onlineTime: null,
        keepaliveTime: null,
        projectId: null,
        firmwareId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getDevice(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceId != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + deviceIds + '"的数据项？').then(function() {
        return delDevice(deviceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('irtu/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
